# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/9/22 18:13
# @Author  : Dell
# @File    : long_summary.py
# @Software: PyCharm
# @Desc    :长文本进行总结
# 步骤：加载文本》切割文本》

from langchain.chains.summarize import load_summarize_chain
from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model_name='qwen-plus', base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", temperature=0.5,
                 api_key="sk-7b4f290b1a3e42a1a9a1957fa44eff37")

loader = TextLoader(file_path="xiyouji.txt",encoding="utf-8")
documents = loader.load()

# 初始化分割器
text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=50)
# 文档切分
texts=text_splitter.split_documents(documents)
# 不同总结方法
chain=load_summarize_chain(llm,chain_type="map_reduce",verbose=True)
output=chain.invoke(texts)
print(output)